<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class Main extends Base_controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $sess = $this->session->userdata("logined");
        if ($sess === TRUE) {
            $data = array();
            //set menu
            $data = $this->SetMenu("ThongKe");
            //lay ngay hien tai
            $ngay = date("d");
            $thang = date("m");
            $nam = date("Y");
            $curdate = $nam . "-" . $thang . "-" . $ngay;
            $data["sumHD"] = $this->order_model->SumHD(NULL, NULL);
            $data["sumPd"] = $this->product_model->SumProduct();
            $data["sumFB"] = $this->feed_back_model->SumFB();
            $data["sumTK"] = $this->user_model->SumKH();
            $data["HDMoi"] = $this->order_model->SumHD($curdate, NULL);
            $data["HDCXL"] = $this->order_model->SumHD(NULL, 1);
            $data["HDDangXL"] = $this->order_model->SumHD(NULL, 2);
            $data["HDDaXL"] = $this->order_model->SumHD(NULL, 3);
            $data["SPDB"] = $this->product_model->SumProduct(0);
            $data["SPDTD"] = $this->product_model->SumProduct(1);
            $data["FBCD"] = $this->feed_back_model->SumFB(0);
            $data["FBDD"] = $this->feed_back_model->SumFB(1);
            $data["TKDD"] = $this->user_model->sumKH(1);
            $data["TKBK"] = $this->admin_model->SumTK(1);
            $data["TKHD"] = $this->admin_model->SumTK(0);

            $this->render($this->load->view("admin/common/admin_home_view", $data, TRUE));
        } else {
            $this->load->view("admin/account/login", array());
        }
    }

    public function login_admin() {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database_login');
        if ($this->form_validation->run() === FALSE) {
            redirect("dienhoa-administrator/main");
        } else if ($this->session->userdata('logged_in_admin')) {
            redirect("dienhoa-administrator/main");
        } else {
            redirect("dienhoa-administrator/main");
        }
    }

    function check_database_login($password) {
        $username = $this->input->post("username");
        $result = $this->admin_model->login($username, $password);
        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                $sess_array = array(
                    'ID' => $row->AdminID,
                    'user' => $row->AdminName,
                    'user_name' => $row->AdminUser,
                    'group' => $row->admin_group_id,
                    'logined' => TRUE
                );
                $this->session->set_userdata($sess_array);
            }
            return TRUE;
        } else {
            $this->form_validation->set_message("mat khau hoac tai khoan khong dung");
            return FALSE;
        }
    }

    public function log_out() {
        if ($this->session->userdata("logined") === TRUE) {
            $data = array(
                'ID' => "",
                'user' => "",
                "user_name" => "",
                "group" => "",
                "logined" => FALSE
            );
            $this->session->unset_userdata($data);
            redirect("dienhoa-administrator/main");
        }
    }

}

?>
